package com.graph;
import java.util.ArrayList;

import com.path.algorithms.*;

public class BasicGraph {

	private AdjacencyMatrix am;
	private FindPath fp;
	
	public BasicGraph(){
		this.am = new AdjacencyMatrix();
		this.fp = new FindPath();
	}
	
	
	public BasicGraph(String fileName){
		this.am = new AdjacencyMatrix();
		this.fp = new FindPath();
		this.createMatrixFromFile(fileName);
	}
	
	/**Creates the adjacency matrix from a file 
	 * <br>called "matrix.txt"
	 * */
	public boolean createMatrixFromFile(){
		try{
			am.createMatrixFromFile("matrix.txt");
			return true;
		}catch(Exception E){
			return false;
		}
	}
	
	
	/**Creates the adjacency matrix from the
	 * <br> given file.
	 * */
	public boolean createMatrixFromFile(String fileName){
		try{
			am.createMatrixFromFile(fileName);
			return true;
		}catch(Exception e){
			return false;
		}
		
	}

	/**Returns an AdjacencyMatrix object.
	 * <br>
	 * <br> This will probably be taken out
	 * <br> in future versions of this file.
	 * */
	public AdjacencyMatrix getAdjacencyMatrix(){
		return this.am;
	}
	
	
	
	/**Returns an array list for the given vertex
	 * <br>This is essentially a row from the Adjacency
	 * <br>Matrix that describes what other vertices
	 * <br>the given vertex is adjacent to.
	 * <br>
	 * <br> 0 for not adjacent, 
	 * <br> 1 for adjacent.
	 * */
	public ArrayList<Integer> getVertexAdjacency(int n){
		return am.getVertex(n);
	}
	
	
	
	/**Uses a depth-first search algorithm to find
	 * <br>a path between two given vertices if such
	 * <br>a path exists.
	 * <br>
	 * <br>This path is NOT guaranteed to be the
	 * <br>shortest path.
	 * */
	public ArrayList<Integer> findPath(int start, int end){
		this.fp.find(this.am, start, end);
		return this.fp.getPath();
	}
	
	
	
	/**This function attempts to find shortcuts in the
	 * <br>given path.
	 * <br>
	 * <br>For example if the given path is [1,5,2,7,8]
	 * <br>and an edge exists between 1 and 7, this
	 * <br>function will return the following 
	 * <br>ArrayList:
	 * <br>
	 * <br>[1,7,8]
	 * <br>
	 * <br>This function is not guaranteed to find the shortest
	 * <br>path between two vertices, but it will find all
	 * <br>shortcuts in a given path.
	 * */
	public ArrayList<Integer> improvePath(ArrayList<Integer> path){
		return this.fp.improvePath(path);
	}

}
